extends "enemy.gd"


#var controller
@export var max_hp = 4
var can_shoot = true
var aggro = false
var timer
var bullet = preload("res://scenes/bullet.tscn")

var anim
var sprite

func getDamageType():
	return "hurtful"

func _ready():
	super._ready()
	timer = get_node("Timer")
	anim = get_node("AnimationPlayer")
	sprite = get_node("Sprite")
	hp = max_hp

func _physics_process(delta):
#	print("c:"+str(can_shoot)+" a:"+str(aggro))
	if (global_position.x < player.global_position.x):
		sprite.scale.x = 1.0
	else:
		sprite.scale.x = -1
		set_scale(Vector2(-1,1))
	if (aggro and can_shoot):
		can_shoot = false
		timer.start()
		anim.play("shoot")
		anim.queue("idle")

func shoot():
	var pos = get_node("Sprite/Position2D").global_position
	var inst = bullet.instantiate()
	inst.global_position = pos
	var parent = get_parent()
	while (parent.get_node("Player") == null):
		parent = parent.get_parent()
		print(parent.get_name())
	parent.add_child(inst)
	

func _on_Timer_timeout():
	can_shoot = true

func _on_Aggro_body_enter( body ):
	if (body == player):
		aggro = true
		
func _on_Aggro_body_exit( body ):
	if (body == player):
		aggro = false
